#include <iostream>
#include "../_Work/CKDatabase.h"


void Insert(K_Engine::CKDatabase *db);
void Delete(K_Engine::CKDatabase *db);
void Update(K_Engine::CKDatabase *db);
void Select(K_Engine::CKDatabase *db);

int main(int argc, char** argv)
{
	K_Engine::CKDatabase db;
	db.SetHost("localhost");
	db.SetUserPasswork("root", "root");
	db.SetDBname("test");
	
	//连接
	if(!db.connectDB())
	{
		std::cout<<"连接数据库失败"<<std::endl;
	}
	else
	{
		// Insert(&db);
		// Select(&db);
		// Update(&db);
		// Select(&db);
		// Delete(&db);
		Select(&db);
	}
	
	return 0;
}

void Insert(K_Engine::CKDatabase *db)
{
	if(!db->Query("insert into t(name,age)values(\"小胡\",3)"))
	{
		std::cout<<"插入数据失败"<<std::endl;
	}
	else
	{
		std::cout<<"插入数据成功"<<std::endl;
	}
}


void Delete(K_Engine::CKDatabase *db)
{
	if(!db->Query("delete from t"))
	{
		std::cout<<"删除数据失败"<<std::endl;
	}
	else
	{
		std::cout<<"删除数据成功"<<std::endl;
	}
}


void Update(K_Engine::CKDatabase *db)
{
	if(!db->Query("update t set name='大胡',age=30 where name='小胡'"))
	{
		std::cout<<"修改数据失败"<<std::endl;
	}
	else
	{
		std::cout<<"修改数据成功"<<std::endl;
	}
}


void Select(K_Engine::CKDatabase *db)
{
	K_Engine::CKDBSelectResult* result = db->Select("select * from t");	
	if(NULL == result)
	{
		std::cout<<"查询出错"<<std::endl;
	}
	else
	{
		std::cout<<"查询成功"<<std::endl;
		int listlength = result->nGetResultListLength();
		int resultlength = result->nGetResultLength();
		for(int i = 0 ; i < resultlength ; i++)
		{
			for(int j = 0 ; j < listlength ; j++)
			{
				std::cout<<(*(result->GetLine(i)))[j]<<"--";
			}
			std::cout<<std::endl;
		}
	}
	db->FreeCKDBSR(result);
}